package com.chuanyang.driverh.mainorder;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviPath;
import com.amap.api.navi.model.AMapNaviStaticInfo;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.navi.view.RouteOverLay;
import com.autonavi.tbt.NaviStaticInfo;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.bean.MarKerPointBean;
import com.chuanyang.driverh.mainorder.adapter.MarkerPoinAdapter;
import com.chuanyang.driverh.mainorder.adapter.RoteMethodAdapter;
import com.chuanyang.driverh.mainother.GpsNaviActivity;
import com.chuanyang.driverh.responseresult.OrderData;
import com.chuanyang.driverh.responseresult.OrderDetailData;
import com.chuanyang.driverh.responseresult.WaybillDetailsData;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.List;

public class MarkerActivity extends Activity implements View.OnClickListener, AMap.OnMarkerDragListener, AMap.OnMapLoadedListener, AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter, AMapLocationListener {
    private MapView mapView;
    private TextView markerText;
    private Button markerButton1, markerButton2;
    private AMap aMap;
    private MarkerOptions markerOption;
    private LatLng XIAN;
    private ListView mListView;
    private PopupWindow popupWindow;
    private String[] data;
    private LatLng CHENDU;
    private TextView mBaseTitle;
    private TextView mBaseBack;
    private Button marker_linesmethod;
    private LinearLayout ll_marker;
    private int method;
    private float range;
    private boolean cheselines = false;
    private AMapNavi mAMapNavi;
    List<NaviLatLng> mStartList = new ArrayList<NaviLatLng>();
    List<NaviLatLng> mEndList = new ArrayList<NaviLatLng>();
    List<NaviLatLng> mWayPointList;
    private NaviLatLng mStartLatlng;
    private NaviLatLng mEndLatlng;
    private boolean isOrder;
    private OrderData.DataBean orderdata;
    private OrderDetailData.PassPointsBean orderStartPoint;
    private OrderDetailData.PassPointsBean orderEndPoint;
    private WaybillDetailsData.PassPointsBean wayBillStartPoint;
    private WaybillDetailsData.PassPointsBean wayBillEndPoint;
    private List<OrderDetailData.PassPointsBean> orderPasspointlist;
    private List<WaybillDetailsData.PassPointsBean> wayBillPasspointlist;
    private ArrayList<LatLng> latLngs;
    private List<OrderDetailData.PassPointsBean> orderPointlist;
    private List<WaybillDetailsData.PassPointsBean> wayBillPointlist;
    private List<MarKerPointBean> markerPointlist;
    private LatLng latLng;
    private LatLng centerLatLng;
    private OrderDetailData.PassPointsBean checkOrderPoint;
    private WaybillDetailsData.PassPointsBean checkWaybillPoint;
    private TextView tv_act;
    private TextView tv_add;
    private AMapLocationClient locationClient;
    private AMapLocationClientOption locationOption;
    private AMapLocation mMapLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_marker);
        //获取其他页面传递来的数据
        getData();
        initLocation();
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState); // 此方法必须重写
        mAMapNavi = AMapNavi.getInstance(getApplicationContext());
        // startService(new Intent(this, GpsService.class));
        mAMapNavi.addAMapNaviListener(new AMapNaviListener() {
            @Override
            public void onInitNaviFailure() {

            }

            @Override
            public void onInitNaviSuccess() {

            }

            @Override
            public void onStartNavi(int i) {

            }

            @Override
            public void onTrafficStatusUpdate() {

            }

            @Override
            public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

            }

            @Override
            public void onGetNavigationText(int i, String s) {

            }

            @Override
            public void onEndEmulatorNavi() {

            }

            @Override
            public void onArriveDestination() {

            }

            @Override
            public void onArriveDestination(NaviStaticInfo naviStaticInfo) {

            }

            @Override
            public void onArriveDestination(AMapNaviStaticInfo aMapNaviStaticInfo) {

            }

            @Override
            public void onCalculateRouteSuccess() {
                AMapNaviPath naviPath = mAMapNavi.getNaviPath();
                if (naviPath == null) {
                    return;
                }
                RouteOverLay mRouteOverLay = new RouteOverLay(aMap, naviPath, MarkerActivity.this);
                mRouteOverLay.setAMapNaviPath(naviPath);
                LogUtils.e("RouteOverLay", "RouteOverLay");
                mRouteOverLay.addToMap();
            }

            @Override
            public void onCalculateRouteFailure(int i) {

            }

            @Override
            public void onReCalculateRouteForYaw() {

            }

            @Override
            public void onReCalculateRouteForTrafficJam() {

            }

            @Override
            public void onArrivedWayPoint(int i) {

            }

            @Override
            public void onGpsOpenStatus(boolean b) {

            }

            @Override
            public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

            }

            @Override
            public void onNaviInfoUpdate(NaviInfo naviInfo) {

            }

            @Override
            public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

            }

            @Override
            public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

            }

            @Override
            public void showCross(AMapNaviCross aMapNaviCross) {

            }

            @Override
            public void hideCross() {

            }

            @Override
            public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

            }

            @Override
            public void hideLaneInfo() {

            }

            @Override
            public void onCalculateMultipleRoutesSuccess(int[] ints) {

            }

            @Override
            public void notifyParallelRoad(int i) {

            }

            @Override
            public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

            }

            @Override
            public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

            }

            @Override
            public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

            }
        });


        init();
        //naviFlag为AMapNavi.GPSNaviMode表示真实导航，naviFlag为AMapNavi.EmulatorNaviMode表示模拟导航
        //  AMapNavi.getInstance(this).startNavi(NaviType.GPS);
    }

    private void initLocation() {
        if (locationClient == null) {
            locationClient = new AMapLocationClient(this.getApplicationContext());
        }
        if (locationOption == null) {
            locationOption = new AMapLocationClientOption();
        }
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        // 设置定位监听
        locationClient.setLocationListener(this);
        //配置locationOption的参数信息
        setOptions();
        // 设置定位参数
        locationClient.setLocationOption(locationOption);
        // 启动定位
        locationClient.startLocation();
    }

    private void setOptions() {
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //只有持续定位设置定位间隔才有效，单次定位无效
        locationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        locationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        locationOption.setMockEnable(false);
        locationOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
        locationOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        // 设置是否需要显示地址信息
        locationOption.setNeedAddress(true);
        /**
         * 设置是否优先返回GPS定位结果，如果30秒内GPS没有返回定位结果则进行网络定位
         * 注意：只有在高精度模式下的单次定位有效，其他方式无效
         */
        locationOption.setGpsFirst(true);
        locationOption.setInterval(3000);
    }

    private void getData() {
        Intent intent = getIntent();
        latLngs = new ArrayList();
        markerPointlist = new ArrayList();

        if (intent.getSerializableExtra("data") instanceof OrderDetailData) {
            isOrder = true;
            OrderDetailData orderdata = (OrderDetailData) intent.getSerializableExtra("data");
            orderPointlist = orderdata.getPassPoints();
            orderPasspointlist = new ArrayList();
            for (OrderDetailData.PassPointsBean passPointsBean : orderPointlist) {
                double latitude = Double.parseDouble(passPointsBean.getLatitude());
                double lngitude = Double.parseDouble(passPointsBean.getLongitude());
                latLng = new LatLng(latitude, lngitude);
                MarKerPointBean marKerPointBean = new MarKerPointBean();
                marKerPointBean.setMarkerId(passPointsBean.getPointId() + "");
                marKerPointBean.setLatlng(latLng);
                marKerPointBean.setOrderPassPoint(passPointsBean);
                if (passPointsBean.getPointType().equals("s")) {
                    orderStartPoint = passPointsBean;
                } else if (passPointsBean.getPointType().equals("e")) {
                    orderEndPoint = passPointsBean;

                } else {
                    orderPasspointlist.add(passPointsBean);
                }
                latLngs.add(latLng);
                markerPointlist.add(marKerPointBean);

            }
        } else if (intent.getSerializableExtra("data") instanceof WaybillDetailsData) {
            isOrder = false;
            WaybillDetailsData wayBillData = (WaybillDetailsData) intent.getSerializableExtra("data");
            wayBillPointlist = wayBillData.getPassPoints();
            wayBillPasspointlist = new ArrayList();
            for (WaybillDetailsData.PassPointsBean passPointsBean : wayBillPointlist) {
                double latitude = Double.parseDouble(passPointsBean.getLatitude());
                double lngitude = Double.parseDouble(passPointsBean.getLongitude());
                latLng = new LatLng(latitude, lngitude);
                MarKerPointBean marKerPointBean = new MarKerPointBean();
                marKerPointBean.setMarkerId(passPointsBean.getPointId() + "");
                marKerPointBean.setLatlng(latLng);
                marKerPointBean.setWaybillPassPoint(passPointsBean);

                if (passPointsBean.getPointType().equals("s")) {
                    wayBillStartPoint = passPointsBean;
                } else if (passPointsBean.getPointType().equals("p")) {
                    wayBillPasspointlist.add(passPointsBean);


                } else {
                    wayBillEndPoint = passPointsBean;

                }
                latLngs.add(latLng);
                markerPointlist.add(marKerPointBean);
            }


        }
    }

    private void init() {
        //   markerText = (TextView) findViewById(R.id.mark_listenter_text);
        mBaseTitle = (TextView) findViewById(R.id.base_titlebar_text);
        mBaseBack = (TextView) findViewById(R.id.base_titlebar_back);
        markerButton1 = (Button) findViewById(R.id.marker_button1);
        markerButton2 = (Button) findViewById(R.id.marker_button2);
        marker_linesmethod = (Button) findViewById(R.id.marker_linesmethod);
        ll_marker = (LinearLayout) findViewById(R.id.ll_marker);
        mBaseTitle.setText("导航");
        setBackImg();
        mBaseBack.setOnClickListener(this);
        markerButton1.setOnClickListener(this);
        markerButton2.setOnClickListener(this);
        marker_linesmethod.setOnClickListener(this);
        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }
        // addPolylinessoild();//绘制一个连线


    }

    /**
     * title返回图片按钮，此处开发者可自定义设置图片资源
     */
    protected void setBackImg() {
        mBaseBack.setVisibility(View.VISIBLE);

        // 初始化返回按钮图片大小
//        UiHelpers.setTextViewIcon(this, mBaseBack, R.mipmap.ic_back,
//                R.dimen.common_titlebar_icon_width,
//                R.dimen.common_titlebar_icon_height, UiHelpers.DRAWABLE_LEFT);
    }

    private void setUpMap() {
        aMap.setOnMarkerDragListener(this);// 设置marker可拖拽事件监听器
        aMap.setOnMapLoadedListener(this);// 设置amap加载成功事件监听器
        aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
        aMap.setOnInfoWindowClickListener(this);// 设置点击infoWindow事件监听器
        aMap.setInfoWindowAdapter(this);
        addMarkersToMap();// 往地图上添加marker
    }

    private void addMarkersToMap() {
        // 动画效果
        ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>();
        giflist.add(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
        giflist.add(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_RED));
        giflist.add(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
        ArrayList<MarkerOptions> markerOptionlst = new ArrayList<MarkerOptions>();
        if (isOrder) {
            for (int i = 0; i < orderPointlist.size(); i++) {
                MarKerPointBean marKerPointBean = markerPointlist.get(i);
                latLng = latLngs.get(i);
                markerOption = new MarkerOptions();
                markerOption.anchor(0.5f, 0.5f)
                        .position(latLng).title(marKerPointBean.getMarkerId()).snippet("成都市：30.679879, 104.064855").icons(giflist)  //设置 title为途经点id
                        .draggable(true).period(10).visible(true);
                markerOptionlst.add(markerOption);
                aMap.addMarkers(markerOptionlst, true);        //是否让marker在屏幕中间


            }
            centerLatLng = new LatLng((Double.parseDouble(orderStartPoint.getLatitude()) + Double.parseDouble(orderEndPoint.getLatitude())) / 2, (Double.parseDouble(orderStartPoint.getLongitude()) + Double.parseDouble(orderEndPoint.getLongitude())) / 2);

        } else {
            for (int i = 0; i < wayBillPointlist.size(); i++) {
                MarKerPointBean marKerPointBean = markerPointlist.get(i);
                latLng = latLngs.get(i);
                markerOption = new MarkerOptions();
                markerOption.anchor(0.5f, 0.5f)
                        .position(latLng).title(marKerPointBean.getMarkerId()).snippet("成都市：30.679879, 104.064855").icons(giflist)
                        .draggable(true).period(10).visible(true);
                markerOptionlst.add(markerOption);
                aMap.addMarkers(markerOptionlst, true);        //是否让marker在屏幕中间


            }
            centerLatLng = new LatLng((Double.parseDouble(wayBillStartPoint.getLatitude()) + Double.parseDouble(wayBillEndPoint.getLatitude())) / 2, (Double.parseDouble(wayBillStartPoint.getLongitude()) + Double.parseDouble(wayBillEndPoint.getLongitude())) / 2);


        }

        adjustCamera(centerLatLng, getZoomRange());


    }

    //   / ** * 调节地图到正好放置查询范围的所有点 * @param centerLatLng 中心点 * @param range 查询范围（米） */
    private void adjustCamera(LatLng centerLatLng, int range) {
        //http://www.eoeandroid.com/blog-1107295-47621.html
        //当前缩放级别下的比例尺
        //"每像素代表" + scale + "米"
        float scale = aMap.getScalePerPixel();
        //代表range（米）的像素数量
        int pixel = Math.round(range / scale);
        //小范围，小缩放级别（比例尺较大），有精度损失
        Projection projection = aMap.getProjection();
        //将地图的中心点，转换为屏幕上的点
        Point center = projection.toScreenLocation(centerLatLng);
        //获取距离中心点为pixel像素的左、右两点（屏幕上的点
        Point right = new Point(center.x + pixel, center.y);
        Point left = new Point(center.x - pixel, center.y);
        //将屏幕上的点转换为地图上的点
        LatLng rightLatlng = projection.fromScreenLocation(right);
        LatLng LeftLatlng = projection.fromScreenLocation(left);
        //调整可视范围
        aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(LatLngBounds.builder().include(rightLatlng).include(LeftLatlng).build(), 10));

    }


    private int getZoomRange() {
        //缩放级别
        float zoom = aMap.getCameraPosition().zoom;
        //"每像素代表" + scale + "米"
        float scale = aMap.getScalePerPixel();
        range = scale * zoom;
        LogUtils.e("rangge", range + "-------");
        return (int) range;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.marker_button1:
                showPopuWindow(1);
                break;
            case R.id.marker_linesmethod:
                showPopuWindow(2);
                break;
            case R.id.base_titlebar_back:
                finish();
                break;
            case R.id.marker_button2:
                if (mMapLocation != null) {
                    if (isOrder) {
                        if (checkOrderPoint != null) {

                            Intent intent = new Intent(this, GpsNaviActivity.class);
                            intent.putExtra("method", method);
                            intent.putExtra("endlongtitude", Double.parseDouble(checkOrderPoint.getLongitude()));
                            intent.putExtra("endlatitude", Double.parseDouble(checkOrderPoint.getLatitude()));
                            startActivity(intent);

                        } else {
                            PrintUtil.toast(MarkerActivity.this, "请选择目的地");
                        }
                    } else {
                        if (checkWaybillPoint != null) {
                            Intent intent = new Intent(this, GpsNaviActivity.class);
                            intent.putExtra("method", method);
                            intent.putExtra("endlongtitude", Double.parseDouble(checkWaybillPoint.getLongitude()));
                            intent.putExtra("endlatitude", Double.parseDouble(checkWaybillPoint.getLatitude()));
                            startActivity(intent);
                        } else {
                            PrintUtil.toast(MarkerActivity.this, "请选择目的地");

                        }

                    }

                } else {
                    PrintUtil.toast(MarkerActivity.this, "请稍后！正在开启定位！");
                }
                break;
            case R.id.tv_act_marker_act:
                break;
        }
    }

    @Override
    public void onMarkerDragStart(Marker marker) {

        //  markerText.setText(marker.getTitle() + "开始拖动");
    }

    @Override
    public void onMarkerDrag(Marker marker) {
        String curDes = marker.getTitle() + "拖动时当前位置:(lat,lng)\n("
                + marker.getPosition().latitude + ","
                + marker.getPosition().longitude + ")";
        //   markerText.setText(curDes);

    }

    @Override
    public void onMarkerDragEnd(Marker marker) {
        markerText.setText(marker.getTitle() + "停止拖动");
    }

    @Override
    public void onMapLoaded() {
        // 设置所有maker显示在当前可视区域地图中
//        LatLngBounds bounds = new LatLngBounds.Builder()
//                .include(XIAN).build();
//        //此api的作用是让该点处于地图中间位置
//        aMap.getScalePerPixel();
//        aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
//        if (aMap != null) {
//            jumpPoint(marker);
//        }
        return false;
    }


    private void showPopuWindow(int index) {
        /**
         * 弹出一个下拉窗体
         */

        initListView(index);
        // 创建PopupWindow对象，指定内容为ListView，宽度为输入框的宽度，高度为200，当前窗体可以获取焦点
        popupWindow = new PopupWindow(mListView, ll_marker.getWidth() - 8, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(ll_marker, 4, -4);
    }


    private void initListView(final int index) {
        mListView = new ListView(this);
//        mListView.setVerticalScrollBarEnabled(false); // 隐藏垂直滚动条
//        mListView.setDividerHeight(0); // 去除分割线
        mListView.setBackgroundResource(R.drawable.rectangle_corner_shape);
        if (index == 1) {
            if (isOrder) {
                MarkerPoinAdapter adapter = new MarkerPoinAdapter(this, orderPointlist, true);
                mListView.setAdapter(adapter);
            } else {
                MarkerPoinAdapter adapter = new MarkerPoinAdapter(this, wayBillPointlist, false);
                mListView.setAdapter(adapter);
            }


        } else {
            data = new String[]{"躲避拥堵", "不走高速", "避免收费", "高速优先"};
            RoteMethodAdapter adapter = new RoteMethodAdapter(this, data);//data既可以是数组，也可以是List集合
            mListView.setAdapter(adapter);


        }

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            private int strategy;

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popupWindow.dismiss();
                if (index == 1) {
                    String address = "";
                    if (isOrder) {

                        checkOrderPoint = orderPointlist.get(position);
                        if (!TextUtils.isEmpty(checkOrderPoint.getCity())) {
                            address = address + checkOrderPoint.getCity();
                        }
                        if (!TextUtils.isEmpty(checkOrderPoint.getRegions())) {
                            address = address + checkOrderPoint.getRegions();
                        }

                        markerButton1.setText(address);

                        markerButton1.setText(checkOrderPoint.getCity());
                    } else {
                        checkWaybillPoint = wayBillPointlist.get(position);
                        if (!TextUtils.isEmpty(checkWaybillPoint.getCity())) {
                            address = address + checkWaybillPoint.getCity();
                        }
                        if (!TextUtils.isEmpty(checkWaybillPoint.getRegions())) {
                            address = address + checkWaybillPoint.getRegions();
                        }
                        markerButton1.setText(address);

                    }
                    cheselines = true;

                } else {
                    if (mMapLocation != null) {

                        if (cheselines) {//如果已经选定终点
                            if (position == 0) {
                                method = 1;
                                try {
                                    strategy = mAMapNavi.strategyConvert(true, false, false, false, false);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            } else if (position == 1) {
                                method = 2;
                                try {
                                    strategy = mAMapNavi.strategyConvert(true, false, false, false, false);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else if (position == 2) {
                                method = 3;
                                try {
                                    strategy = mAMapNavi.strategyConvert(true, false, false, false, false);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                method = 3;
                                try {
                                    strategy = mAMapNavi.strategyConvert(true, false, false, false, false);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            marker_linesmethod.setText(data[position]);
                            if (isOrder) {
                                mEndLatlng = new NaviLatLng(Double.parseDouble(checkOrderPoint.getLatitude()), Double.parseDouble(checkOrderPoint.getLongitude()));

                            } else {
                                mEndLatlng = new NaviLatLng(Double.parseDouble(checkWaybillPoint.getLatitude()), Double.parseDouble(checkWaybillPoint.getLongitude()));


                            }
                            mStartLatlng = new NaviLatLng(Double.parseDouble(SharedPrefUtil.getLatitude()), Double.parseDouble(SharedPrefUtil.getLongitude()));
                            mStartList.add(mStartLatlng);
                            mEndList.add(mEndLatlng);
                            centerLatLng = new LatLng((mStartLatlng.getLatitude() + mEndLatlng.getLatitude()) / 2, (mStartLatlng.getLongitude() + mEndLatlng.getLongitude()) / 2);
                            //  adjustCamera(centerLatLng, 1);
//                       aMap.moveCamera( CameraUpdateFactory.newCameraPosition(new CameraPosition(
//                               centerLatLng, 0, 0, 0)));
                            aMap.moveCamera(CameraUpdateFactory.changeLatLng(centerLatLng));
                            mAMapNavi.calculateDriveRoute(mStartList, mEndList, mWayPointList, strategy);

                        } else {
                           PrintUtil.toast(MarkerActivity.this, "请选定目的地");
                        }
                    } else {

                        PrintUtil.toast(MarkerActivity.this, "正在获取当前位置，请稍后！");
                    }
                }
            }

        });


        // mListView.setAdapter(adapter);

    }


    @Override
    public View getInfoWindow(Marker marker) {
        View infoContent = getLayoutInflater().inflate(
                R.layout.act_marker_custom, null);
        tv_add = (TextView) infoContent.findViewById(R.id.tv_act_marker_address);
        tv_act = (TextView) infoContent.findViewById(R.id.tv_act_marker_act);
        for (int i = 0; i < markerPointlist.size(); i++) {
            MarKerPointBean marKerPointBean = markerPointlist.get(i);
            if (marKerPointBean.getMarkerId().equals(marker.getTitle())) {
                MarKerPointBean checkMarkerBean = marKerPointBean;

                if (isOrder) {
                    String address = "";
                    checkOrderPoint = checkMarkerBean.getOrderPassPoint();
                    if (!TextUtils.isEmpty(checkOrderPoint.getCity())) {
                        address = address + checkOrderPoint.getCity();
                    }
                    if (!TextUtils.isEmpty(checkOrderPoint.getRegions())) {
                        address = address + checkOrderPoint.getRegions();
                    }

                    tv_add.setText(address);
                } else {
                    String address = "";

                    checkWaybillPoint = checkMarkerBean.getWaybillPassPoint();
                    if (!TextUtils.isEmpty(checkWaybillPoint.getCity())) {
                        address = address + checkWaybillPoint.getCity();
                    }
                    if (!TextUtils.isEmpty(checkWaybillPoint.getRegions())) {
                        address = address + checkWaybillPoint.getRegions();
                    }

                    tv_add.setText(address);

                }

            }

        }

        tv_act.setOnClickListener(this);
        return infoContent;

    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;

    }


    @Override
    public void onInfoWindowClick(Marker marker) {


        if (marker.isInfoWindowShown()) {
            if (isOrder) {
                markerButton1.setText(checkOrderPoint.getCity());
                cheselines = true;
            } else {
                markerButton1.setText(checkWaybillPoint.getCity());
                cheselines = true;


            }
            marker.hideInfoWindow();

        } else {
            marker.showInfoWindow();
        }
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        if (null != locationClient) {
            /**
             * 关闭服务时释放资源
             * onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            // 停止定位
            locationClient.stopLocation();
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (null != aMapLocation) {
            if (aMapLocation.getErrorCode() == 0) {
                LogUtils.e("location", aMapLocation.getLongitude() + "-----" + aMapLocation.getLatitude() + "");
                SharedPrefUtil.setLongitude(aMapLocation.getLongitude() + "");
                SharedPrefUtil.setLatitude(aMapLocation.getLatitude() + "");
                mMapLocation = aMapLocation;

            } else {
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }
}
